
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="Python">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>objective &#8212; deepaugment 0.2.0 documentation</title>
    <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for objective</h1><div class="highlight"><pre>
<span></span><span class="c1"># (C) 2019 Baris Ozmen &lt;hbaristr@gmail.com&gt;</span>

<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">deepaugment.augmenter</span> <span class="k">import</span> <span class="n">augment_by_policy</span>
<span class="kn">from</span> <span class="nn">deepaugment.lib.helpers</span> <span class="k">import</span> <span class="n">log_and_print</span>


<div class="viewcode-block" id="Objective"><a class="viewcode-back" href="../objective.html#objective.Objective">[docs]</a><span class="k">class</span> <span class="nc">Objective</span><span class="p">():</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">data</span><span class="p">,</span>
        <span class="n">child_model</span><span class="p">,</span>
        <span class="n">notebook</span><span class="p">,</span>
        <span class="n">config</span>
    <span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">child_model</span> <span class="o">=</span> <span class="n">child_model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opt_samples</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s2">&quot;opt_samples&quot;</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opt_last_n_epochs</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s2">&quot;opt_last_n_epochs&quot;</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">notebook</span> <span class="o">=</span> <span class="n">notebook</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logging</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s2">&quot;logging&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="Objective.evaluate"><a class="viewcode-back" href="../objective.html#objective.Objective.evaluate">[docs]</a>    <span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_no</span><span class="p">,</span> <span class="n">trial_hyperparams</span><span class="p">):</span>

        <span class="n">augmented_data</span> <span class="o">=</span> <span class="n">augment_by_policy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;X_train&quot;</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;y_train&quot;</span><span class="p">],</span> <span class="o">*</span><span class="n">trial_hyperparams</span><span class="p">)</span>

        <span class="n">sample_rewards</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">sample_no</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">child_model</span><span class="o">.</span><span class="n">load_pre_augment_weights</span><span class="p">()</span>
            <span class="c1"># TRAIN</span>
            <span class="n">history</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">child_model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">augmented_data</span><span class="p">)</span>
            <span class="c1">#</span>
            <span class="n">reward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">calculate_reward</span><span class="p">(</span><span class="n">history</span><span class="p">)</span>
            <span class="n">sample_rewards</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reward</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">notebook</span><span class="o">.</span><span class="n">record</span><span class="p">(</span><span class="n">trial_no</span><span class="p">,</span> <span class="n">trial_hyperparams</span><span class="p">,</span> <span class="n">sample_no</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">history</span><span class="p">)</span>

        <span class="n">trial_cost</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">sample_rewards</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">notebook</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>

        <span class="n">log_and_print</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;{str(trial_no)}, {str(trial_cost)}, {str(trial_hyperparams)}&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">logging</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">trial_cost</span></div>

<div class="viewcode-block" id="Objective.calculate_reward"><a class="viewcode-back" href="../objective.html#objective.Objective.calculate_reward">[docs]</a>    <span class="k">def</span> <span class="nf">calculate_reward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">history</span><span class="p">):</span>
        <span class="n">history_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">history</span><span class="p">)</span>
        <span class="n">history_df</span><span class="p">[</span><span class="s2">&quot;acc_overfit&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">history_df</span><span class="p">[</span><span class="s2">&quot;acc&quot;</span><span class="p">]</span> <span class="o">-</span> <span class="n">history_df</span><span class="p">[</span><span class="s2">&quot;val_acc&quot;</span><span class="p">]</span>
        <span class="n">reward</span> <span class="o">=</span> <span class="p">(</span><span class="n">history_df</span><span class="p">[</span><span class="n">history_df</span><span class="p">[</span><span class="s2">&quot;acc_overfit&quot;</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mf">0.05</span><span class="p">][</span><span class="s2">&quot;val_acc&quot;</span><span class="p">]</span>
                    <span class="o">.</span><span class="n">nlargest</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opt_last_n_epochs</span><span class="p">)</span>
                    <span class="o">.</span><span class="n">mean</span><span class="p">()</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">reward</span></div></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">deepaugment</a></h1>








<h3>Navigation</h3>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../index.html">Documentation overview</a><ul>
  <li><a href="index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2019, Baris Ozmen.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.3</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>